<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-input v-model="search_word" size="mini" placeholder="请输入用户名" class="handle-input"></el-input>
        <el-button type="primary" @click="dialogVisible = true">添加歌手</el-button>
        <el-button type="primary" @click="delAllButton">批量删除</el-button>
      </div>
      <el-dialog title="添加用户" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="ruleForm.sex">
              <el-radio :label="0">女</el-radio>
              <el-radio :label="1">男</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="手机号" prop="phoneNum">
            <el-input v-model="ruleForm.phoneNum"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>
          <el-form-item label="生日" required>
            <el-col :span="11">
              <el-form-item prop="birth">
                <el-date-picker type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"  placeholder="选择日期" v-model="ruleForm.birth" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="地区" prop="location">
            <el-input v-model="ruleForm.location"></el-input>
          </el-form-item>
          <el-form-item label="签名" prop="introduction">
            <el-input type="textarea" v-model="ruleForm.introduction"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addConsumer">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="修改歌手" :visible.sync="editVisible" width="30%" :before-close="handleClose">
        <el-form :model="editForm" :rules="rules" ref="editForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="editForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="editForm.password"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="editForm.sex">
              <el-radio :label="0">女</el-radio>
              <el-radio :label="1">男</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="手机号" prop="phoneNum">
            <el-input v-model="editForm.phoneNum"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editForm.email"></el-input>
          </el-form-item>
          <el-form-item label="生日" required>
            <el-col :span="11">
              <el-form-item prop="birth">
                <el-date-picker type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"  placeholder="选择日期" v-model="editForm.birth" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="地区" prop="location">
            <el-input v-model="editForm.location"></el-input>
          </el-form-item>
          <el-form-item label="签名" prop="introduction">
            <el-input type="textarea" v-model="editForm.introduction"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" @click="editConsumer">确 定</el-button>
        </span>
      </el-dialog>



      <div>
        <el-table :data="this.data" ref="multipleTable" size="mini" border style="width: 100%" height="630px"
          @selection-change="delAll">

          <el-table-column type="selection"></el-table-column>
          <el-table-column label="头像" width="180" align="center">
            <template slot-scope="scope">
              <div class="Consumer-image" >
                <img :src="getAvatarUrl(scope.row.avator)" alt="" style="width: 100%;height: 80px;" />
              </div>
              <el-upload class="upload-demo" :action="uploadUrl(scope.row.id)" :show-file-list="false" :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <el-button type="success," size="mini">更新图片</el-button>
              </el-upload>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="用户名" width="120" align="center">
          </el-table-column>
          <el-table-column prop="sex" label="性别" width="80" align="center">
            <template slot-scope="scope">
              <span v-text="changeSex(scope.row.sex)"></span>
            </template>
          </el-table-column>
          <el-table-column prop="phoneNum" label="手机号" width="120" align="center">
          </el-table-column>
          <el-table-column prop="email" label="邮箱" width="120" align="center">
          </el-table-column>
          <el-table-column label="生日" width="110" align="center">
            <template slot-scope="scope">
              <div>{{attachBirth(scope.row.birth) }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="location" label="地区" width="90" align="center"></el-table-column>
          <el-table-column label="签名" align="center" width="450px">
            <template slot-scope="scope" align="center">
              <p style="height: 100px; overflow: scroll">{{ scope.row.introduction }}</p>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template slot-scope="scope" align="center">
              <el-button type="primary" size="mini" @click="editConsumerButton(scope.row)">修改</el-button>
              <el-button type="primary" size="mini" @click="delConsumerButton(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination @current-change="handleCurrentChange" background layout="total, prev, pager, next"
            :current-page="currentPage" :page-size="pageSize" :total="tableData.length">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    mixin
  } from '../mixins/index.js';

  import moment from 'moment'
  export default {
    mixins: [mixin],
    computed: {
      // 计算当前表格中的数据
      data() {
        return this.tableData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
      }
    },
    data() {
      return {
        multipleSelect: [],
        inx: -1,
        currentPage: 1,
        pageSize: 5,
        tableData: [],
        tempData: [],
        search_word: '',
        dialogVisible: false,
        editVisible: false,
        delVisible: false,
        ruleForm: {
          username: '',
          sex: '',
          birth: '',
          avator: '',
          createDate: '',
          phoneNum: '',
          email: '',
          location: '',
          introduction: '',
        },
        editForm: {
          id: '',
          username: '',
          sex: '',
          birth: '',
          avator: '',
          createDate: '',
          phoneNum: '',
          email: '',
          location: '',
          introduction: '',
        },
        rules: {
          name: [{
            required: true,
            message: '请输入活动名称',
            trigger: 'blur'
          }],
          sex: [{
            required: true,
            message: '请选择性别',
            trigger: 'blur'
          }],
          birth: [{
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'blur'
          }],
          location: [{
            required: true,
            message: '请填写地区',
            trigger: 'blur'
          }],
          introduction: [{
            required: true,
            message: '请填写自我介绍',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      delAll(all) {
        this.multipleSelect = all;
      },
      delAllButton() {
        if(confirm("删除之后无法恢复，谨慎操作！")){
          let ids = [];
          for (let item of this.multipleSelect) {
            ids.push(item.id)
          }
          let data = {
            "ids": ids
          }
          this.axios.post(this.$store.state.HOST + '/consumer/delete', data, {}).then((res) => {
            if (res.data.code == 1) {
              this.$message.success("批量删除成功！");
              this.delVisible = false;
              this.getData();
            } else {
              this.$message.error("删除失败");
            }
          })
        }

      },
      delConsumerButton(id) {
        if(confirm("删除之后无法恢复，确定删除！")){
          let ids = [];
          ids.push(id);
          let data = {
            "ids": ids
          }
          this.axios({
            method: "post",
            url: "http://localhost:8888/music/consumer/delete",
            data: data,
            withCredentials: true,
          }).then((res) => {
            if (res.data.code == 1) {
              this.$message.success("批量删除成功！");
              this.delVisible = false;
              this.getData();
            } else {
              this.$message.error("删除失败");
            }
          })
        }
      },
      editConsumer() {
        let data = new FormData();
        data.append("id", this.editForm.id);
        data.append("password", this.editForm.password);
        data.append("username", this.editForm.username);
        data.append("sex", this.editForm.sex);
        data.append("birth", this.editForm.birth);
        data.append("introduction", this.editForm.introduction);
        data.append("location", this.editForm.location);
        data.append("avator", this.editForm.avator);
        data.append("phoneNum", this.editForm.phoneNum);
        data.append("email", this.editForm.email);
        var jsonData = {};
        data.forEach((value, key) => jsonData[key] = value);
        this.axios({
          method: "post",
          url: "http://localhost:8888/music/consumer/update",
          withCredentials: true,
          data: jsonData
        }).then((res) => {
          if (res.data.code == 1) {
            this.$message.success("修改成功！");
            this.editVisible = false;
            this.getData();
          }
          if (res.data.code == 0) {
            this.$message.success("修改失败！")
          }
        })


      },
      getAvatarUrl(url){
        return this.$store.state.HOST+url
      },
      editConsumerButton(row) {
        this.editForm.username = row.username;
        this.editForm.password = row.password;
        this.editForm.id = row.id;
        this.editForm.avator = row.avator;
        this.editForm.location = row.location;
        this.editForm.introduction = row.introduction;
        this.editForm.sex = row.sex;
        this.editForm.birth = row.birth;
        this.editForm.phoneNum = row.phoneNum;
        this.editForm.email = row.email;
        this.editVisible = true;
      },
      handleCurrentChange(val) {
        this.currentPage = val
      },
      uploadUrl(id) {
        return this.$store.state.HOST + '/consumer/updateConsumerPic?id=' + id
      },
      handleClose(done) {

        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});

      },
      addConsumer() {
        let data = new FormData();
        data.append("username", this.ruleForm.username);
        data.append("sex", this.ruleForm.sex);
        data.append("password", this.ruleForm.pasword);
        data.append("birth", this.ruleForm.birth);
        data.append("introduction", this.ruleForm.introduction);
        data.append("location", this.ruleForm.location);
        data.append("phoneNum", this.ruleForm.phoneNum);
        data.append("email", this.ruleForm.email);
        var jsonData = {};
        data.forEach((value, key) => jsonData[key] = value);
        this.axios({
          method: "post",
          url: "http://localhost:8888/music/consumer/add",
          withCredentials: true,
          data: jsonData
        }).then((res) => {
          if (res.data.code == 1) {
            this.$message.success("添加成功");
            this.dialogVisible = false;
            this.getData();
          }
          if (res.data.code == 0) {
            this.$message.error("添加失败");
          }
        })

      },
      getData() {
        this.tempData = [];
        this.tableData = [];
        this.axios({
          method: "get",
          url: "http://localhost:8888/music/consumer/findAll",
          withCredentials: true,
        }).then((res) => {
          if (res.data.code == 1) {
            this.tableData = res.data.data;
            this.tempData = res.data.data;
          }
          if (res.data.code == 0) {
            this.$notify.error({
              title: res.data.description,
              message: "查询失败",
            });
          }
        })
      }
    },
    watch: {
      search_word: function() {
        if (this.search_word == '') {
          this.tableData = this.tempData;
        } else {
          this.tableData = [];
          this.currentPage = 1;
          for (let item of this.tempData) {
            if (item.username.includes(this.search_word)) {
              this.tableData.push(item);
            }
          }

        }
      },

    },
    created() {
      this.getData();
    }
  };
</script>

<style>
  .handle-box {
    margin-bottom: 5px;
  }

  .Consumer-img {
    width: 100%;
    height: 80px;
    border-radius: 5px;
    margin-bottom: 5px;
    overflow: hidden;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .pagination {
    display: flex;
    justify-content: center;
  }
</style>
